package view;

import com.sun.corba.se.impl.oa.poa.ActiveObjectMap;
import controller.MainController;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import model.Point;
import model.Shape;


public class MainView extends Stage
{
    public static final int BTN_BG_WIDTH = 25;
    public static final int BTN_BG_HEIGHT = 25;

    public static final int INIT_HEIGHT = 875;
    public static final int INIT_WIDTH = 1200;

    private MainController controller;
    private MenuBar menuBar;
    private ToolBar toolBar;
    private Pane pane;
    private TextArea textArea;

    private MenuBar buildMenuBar()
    {
        MenuBar menuBar = new MenuBar();

        //Start
        Menu startMenu = new Menu("Start");

        MenuItem helpItem = new MenuItem("Help");
        helpItem.setOnAction((ActionEvent e)->{
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.titleProperty().set("INFORMATION");
            alert.setHeaderText("Help");
            alert.setContentText(controller.getHelpInformation());
            alert.showAndWait();
        });

        startMenu.getItems().addAll(helpItem);

        //Edit
        Menu editMenu = new Menu("Edit");

        MenuItem submitItem = new MenuItem("TextSubmit");
        submitItem.setAccelerator(KeyCombination.valueOf("ctrl+Enter"));
        submitItem.setOnAction((ActionEvent e)->{
            controller.handleTextSubmit(pane);
            pane.requestFocus();
        });

        MenuItem undoItem = new MenuItem("Undo");
        undoItem.setAccelerator(KeyCombination.valueOf("ctrl+z"));
        undoItem.setOnAction((ActionEvent e)->controller.handleUndo());

        MenuItem combineItem = new MenuItem("Combine");
        combineItem.setOnAction((ActionEvent e)->controller.handleComposite());

        MenuItem resolveItem = new MenuItem("Resolve");
        resolveItem.setOnAction((ActionEvent e)->controller.handleResolve());

        MenuItem copyItem = new MenuItem("Copy");
        copyItem.setAccelerator(KeyCombination.valueOf("ctrl+c"));
        copyItem.setOnAction((ActionEvent e)->controller.handleCopy());

        MenuItem pasteItem = new MenuItem("Paste");
        pasteItem.setAccelerator(KeyCombination.valueOf("ctrl+v"));
        pasteItem.setOnAction((ActionEvent e)->controller.handlePaste());

        MenuItem deleteItem = new MenuItem("Delete");
        deleteItem.setAccelerator(new KeyCodeCombination(KeyCode.BACK_SPACE));
        deleteItem.setOnAction((ActionEvent)->controller.handleDelete());

        editMenu.getItems().addAll(submitItem,undoItem,combineItem,
                                   resolveItem,copyItem,pasteItem,deleteItem);

        menuBar.getMenus().addAll(startMenu,editMenu);
        return menuBar;
    }

    private ToolBar buildToolBar()
    {
        ToolBar toolBar = new ToolBar();

        Button lineBtn= new Button("",
                ViewUtils.getBackgroundImage("resource/line.png",
                        BTN_BG_WIDTH,BTN_BG_HEIGHT));
        lineBtn.setOnMouseClicked((MouseEvent e)->controller.handleShapeBtnClicked(Shape.Line));

        Button triangleBtn= new Button("",
                ViewUtils.getBackgroundImage("resource/triangle.png",
                        BTN_BG_WIDTH,BTN_BG_HEIGHT));
        triangleBtn.setOnMouseClicked((MouseEvent e)->controller.handleShapeBtnClicked(Shape.Triangle));

        Button rectangleBtn= new Button("",
                ViewUtils.getBackgroundImage("resource/rectangle.png",
                        BTN_BG_WIDTH,BTN_BG_HEIGHT));
        rectangleBtn.setOnMouseClicked((MouseEvent e)->controller.handleShapeBtnClicked(Shape.Rectangle));

        Button circleBtn= new Button("",
                ViewUtils.getBackgroundImage("resource/circle.png",
                        BTN_BG_WIDTH,BTN_BG_HEIGHT));
        circleBtn.setOnMouseClicked((MouseEvent e)->controller.handleShapeBtnClicked(Shape.Circle));

        Button ellipseBtn= new Button("",
                ViewUtils.getBackgroundImage("resource/ellipse.png",
                        BTN_BG_WIDTH,BTN_BG_HEIGHT));
        ellipseBtn.setOnMouseClicked((MouseEvent e)->controller.handleShapeBtnClicked(Shape.Ellipse));

        Button expandBtn = new Button("",
                ViewUtils.getBackgroundImage("resource/expand.png",
                        BTN_BG_WIDTH,BTN_BG_HEIGHT));
        expandBtn.setOnMouseClicked((MouseEvent e)->controller.handleExpandBtnClicked());

        Button shrinkBtn = new Button("",
                ViewUtils.getBackgroundImage("resource/shrink.png",
                        BTN_BG_WIDTH,BTN_BG_HEIGHT));
        shrinkBtn.setOnMouseClicked((MouseEvent e)->controller.handleShrinkBtnClicked());

        ColorPicker colorPicker = new ColorPicker(Color.BLACK);
        colorPicker.setOnAction((ActionEvent e)->controller.handleColorPicked(colorPicker.getValue()));

        toolBar.getItems().addAll(lineBtn,rectangleBtn,triangleBtn,circleBtn,
                                  ellipseBtn,expandBtn,shrinkBtn,colorPicker);

        return toolBar;
    }


    public Pane buildPane()
    {
        Pane pane = new Pane();
        pane.setBackground(new Background(new BackgroundFill(Color.WHITE,null,null)));
        pane.setPrefSize(INIT_WIDTH,INIT_HEIGHT);

        pane.setOnMousePressed((MouseEvent e)->controller.handleNewShapeStarted(new Point(e.getX(),e.getY())));

        pane.setOnMouseDragged((MouseEvent e)->controller.handleMouseDragged(new Point(e.getX(),e.getY()),pane));

        pane.setOnMouseReleased((MouseEvent e)->controller.handleNewShapeFinished(new Point(e.getX(),e.getY())));

        return pane;
    }


    public TextArea buildTextArea()
    {
        TextArea textArea = new TextArea();
        textArea.setWrapText(true);
        textArea.setPrefSize(INIT_WIDTH - BTN_BG_WIDTH,BTN_BG_HEIGHT);
        textArea.textProperty().addListener((obs,oldV,newV)->controller.handleTextAreaChanged(newV));
        return textArea;
    }

    public MainView(MainController controller)
    {
        super();

        this.controller = controller;
        //Layout:VBox
        VBox layout = new VBox();

        //菜单栏:开始,文件,编辑
        menuBar = buildMenuBar();

        //工具栏
        toolBar = buildToolBar();

        //pane
        pane = buildPane();

        //text pane
        textArea = buildTextArea();

        layout.getChildren().addAll(menuBar,toolBar,pane,new Separator(),textArea);
        Scene scene = new Scene(layout);

        setScene(scene);
    }
}
